﻿using System;
using Microsoft.Xna.Framework;

namespace EECS395Library
{
    /// <summary>
    /// The event scheduler schedules actions to occur in the future.
    /// </summary>
    public class EventScheduler : GameComponent
    {
        public EventScheduler(Game game) : base(game)
        {
        }

        private readonly DAryHeap<Action> toCall = new DAryHeap<Action>();

        /// <summary>
        /// Schedules an action to be called at the specified time in the future.
        /// </summary>
        public void Schedule(Action action, double secondsFromNow)
        {
            toCall.Insert(action, currentTime + secondsFromNow);
        }

        private double currentTime;

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            currentTime = gameTime.TotalGameTime.TotalSeconds;

            while (toCall.Count > 0 && toCall.FindMin().Priority <= currentTime)
                toCall.ExtractMin().Item();
        }
    }
}